//
//  DestinationViewController.m
//  FreeTravel
//
//  Created by 徐康 on 16/8/26.
//  Copyright © 2016年 KX. All rights reserved.
#import "CommendSearchViewController.h"
#import "FreeTravelNetwork.h"
#import "DestinationViewController.h"
#import "FreeTravelMacro.h"
#import "Masonry.h"
#import "CellOfHotDestination.h"
#import "CellOfOtherDestination.h"
#import "ModelOfSevenContinents.h"
#import "ModelOfContinents.h"
@interface DestinationViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) ModelOfContinents *model;
@property (nonatomic, strong) UIButton *searchButton;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIImageView *imageViewOfMap;
@property (nonatomic, strong) UIButton *buttonOfAsia;
@property (nonatomic, strong) UIButton *buttonOfEurope;
@property (nonatomic, strong) UIButton *buttonOfAfrica;
@property (nonatomic, strong) UIButton *buttonOfAntarctica;
@property (nonatomic, strong) UIButton *buttonOfOceania;
@property (nonatomic, strong) UIButton *buttonOfSouthAmerica;
@property (nonatomic, strong) UIButton *buttonOfNorthAmerica;
@property (nonatomic, strong) UICollectionView *collectionViewOfHotDestination;
@property (nonatomic, strong) UICollectionView *collectionViewOfOtherDestination;
// 第几个洲
@property (nonatomic, assign) NSInteger order;
@end

@implementation DestinationViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.view.backgroundColor = [UIColor whiteColor];
    //    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.navigationController.navigationBar setBarTintColor:COLORFORFREETRAVEL];
    [self.navigationController.navigationBar setTranslucent:NO];
    self.searchButton.hidden = NO;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.searchButton.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.order = 0;
    self.searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _searchButton.frame = CGRectMake(10, 5, SCREEN_WIDTH-20, 34);
    [self.navigationController.navigationBar addSubview:_searchButton];
    [_searchButton setTitle:@"搜索目的地、帖子、特价、FreeTraveler" forState:UIControlStateNormal];
    _searchButton.titleLabel.font = [UIFont systemFontOfSize:13];
    _searchButton.layer.borderColor = [UIColor whiteColor].CGColor;
    _searchButton.layer.borderWidth = 1;
    _searchButton.layer.cornerRadius = 10;
    _searchButton.layer.masksToBounds = YES;
    [_searchButton setImage:[UIImage imageNamed:@"fangdajing"] forState:UIControlStateNormal];
    _searchButton.backgroundColor = COLORFORFREETRAVEL;
    _searchButton.tintColor = [UIColor whiteColor];
    [_searchButton addTarget:self action:@selector(goSearch) forControlEvents:UIControlEventTouchUpInside];
    [self handleData];
    [self createTableView];
    
    
}
- (void)handleData{
    [FreeTravelNetwork GET:@"http://open.qyer.com/qyer/footprint/continent_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&page=1&track_app_channel=App%2520Store&track_app_version=7.0.1&track_device_info=iPhone%25205s&track_deviceid=E9CA001A-5603-4CA8-B2EA-2BE34F691AC6&track_os=ios%25209.3.4&v=1" andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        
        ModelOfSevenContinents *allContinents = [ModelOfSevenContinents modelWithDic:result];
        self.model = allContinents.data[self.order];
        [self.tableView reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
}
- (void)createTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.view.frame.size.height-108) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[CellOfHotDestination class] forCellReuseIdentifier:@"CellOfHotDestination"];
    [self.tableView registerClass:[CellOfOtherDestination class] forCellReuseIdentifier:@"CellOfOtherDestination"];
    
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    self.tableView.tableHeaderView = headView;
    self.imageViewOfMap = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    [headView addSubview:_imageViewOfMap];
    self.imageViewOfMap.image = [UIImage imageNamed:@"map"];
    self.imageViewOfMap.userInteractionEnabled = YES;
    self.buttonOfAsia = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonOfNorthAmerica = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonOfAfrica = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonOfOceania = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonOfAntarctica = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonOfSouthAmerica = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonOfEurope = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.imageViewOfMap addSubview:self.buttonOfAntarctica];
    [self.imageViewOfMap addSubview:self.buttonOfAfrica];
    [self.imageViewOfMap addSubview:self.buttonOfAsia];
    [self.imageViewOfMap addSubview:self.buttonOfNorthAmerica];
    [self.imageViewOfMap addSubview:self.buttonOfOceania];
    [self.imageViewOfMap addSubview:self.buttonOfSouthAmerica];
    [self.imageViewOfMap addSubview:self.buttonOfEurope];
    [self.buttonOfNorthAmerica setTitle:@"北美洲" forState:UIControlStateNormal];
    [self.buttonOfSouthAmerica setTitle:@"南美洲" forState:UIControlStateNormal];
    [self.buttonOfAsia setTitle:@"亚洲" forState:UIControlStateNormal];
    [self.buttonOfAfrica setTitle:@"非洲" forState:UIControlStateNormal];
    [self.buttonOfAntarctica setTitle:@"南极洲" forState:UIControlStateNormal];
    [self.buttonOfOceania setTitle:@"大洋洲" forState:UIControlStateNormal];
    [self.buttonOfEurope setTitle:@"欧洲" forState:UIControlStateNormal];
    [self.buttonOfAsia setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    self.buttonOfAsia.backgroundColor = [UIColor whiteColor];
    self.buttonOfAsia.layer.cornerRadius = 5;
    self.buttonOfAsia.layer.masksToBounds = YES;
    
    [self.buttonOfAfrica setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    self.buttonOfAfrica.backgroundColor = [UIColor whiteColor];
    self.buttonOfAfrica.layer.cornerRadius = 5;
    self.buttonOfAfrica.layer.masksToBounds = YES;
    
    [self.buttonOfAntarctica setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    self.buttonOfAntarctica.backgroundColor = [UIColor whiteColor];
    self.buttonOfAntarctica.layer.cornerRadius = 5;
    self.buttonOfAntarctica.layer.masksToBounds = YES;
    
    [self.buttonOfNorthAmerica setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    self.buttonOfNorthAmerica.backgroundColor = [UIColor whiteColor];
    self.buttonOfNorthAmerica.layer.cornerRadius = 5;
    self.buttonOfNorthAmerica.layer.masksToBounds = YES;
    
    [self.buttonOfOceania setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    self.buttonOfOceania.backgroundColor = [UIColor whiteColor];
    self.buttonOfOceania.layer.cornerRadius = 5;
    self.buttonOfOceania.layer.masksToBounds = YES;
    
    [self.buttonOfEurope setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    self.buttonOfEurope.backgroundColor = [UIColor whiteColor];
    self.buttonOfEurope.layer.cornerRadius = 5;
    self.buttonOfEurope.layer.masksToBounds = YES;
    
    [self.buttonOfSouthAmerica setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    self.buttonOfSouthAmerica.backgroundColor = [UIColor whiteColor];
    self.buttonOfSouthAmerica.layer.cornerRadius = 5;
    self.buttonOfSouthAmerica.layer.masksToBounds = YES;

    self.buttonOfSouthAmerica.titleLabel.font = [UIFont systemFontOfSize:12];
    self.buttonOfAsia.titleLabel.font = [UIFont systemFontOfSize:12];
    self.buttonOfAfrica.titleLabel.font = [UIFont systemFontOfSize:12];
    self.buttonOfAntarctica.titleLabel.font = [UIFont systemFontOfSize:12];
    self.buttonOfEurope.titleLabel.font = [UIFont systemFontOfSize:12];
    self.buttonOfNorthAmerica.titleLabel.font = [UIFont systemFontOfSize:12];
    self.buttonOfOceania.titleLabel.font = [UIFont systemFontOfSize:12];
    
    self.buttonOfOceania.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.buttonOfOceania.layer.borderWidth = 0.5;
    self.buttonOfAfrica.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.buttonOfAfrica.layer.borderWidth = 0.5;
    self.buttonOfAsia.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.buttonOfAsia.layer.borderWidth = 0.5;
    self.buttonOfAntarctica.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.buttonOfAntarctica.layer.borderWidth = 0.5;
    self.buttonOfNorthAmerica.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.buttonOfNorthAmerica.layer.borderWidth = 0.5;
    self.buttonOfEurope.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.buttonOfEurope.layer.borderWidth = 0.5;
    self.buttonOfSouthAmerica.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.buttonOfSouthAmerica.layer.borderWidth = 0.5;
    
    [self.buttonOfNorthAmerica mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_offset(25);
        make.top.equalTo(self.imageViewOfMap.mas_top).mas_offset(15);
        make.left.equalTo(self.imageViewOfMap.mas_left).mas_equalTo(SCREEN_WIDTH*0.1);
    }];
    [self.buttonOfAsia mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_offset(25);
        make.top.equalTo(self.imageViewOfMap.mas_top).mas_offset(25);
        make.right.equalTo(self.imageViewOfMap.mas_right).mas_equalTo(-SCREEN_WIDTH*0.2);
    }];
    [self.buttonOfAfrica mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_offset(25);
        make.centerY.equalTo(self.imageViewOfMap.mas_centerY);
        make.left.equalTo(self.imageViewOfMap.mas_left).mas_offset(SCREEN_WIDTH/2);
    }];
    [self.buttonOfAntarctica mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_offset(25);
        make.right.equalTo(self.imageViewOfMap.mas_right).mas_offset(-SCREEN_WIDTH*0.3);
        make.bottom.equalTo(self.imageViewOfMap.mas_bottom).mas_offset(-25);
    }];
    [self.buttonOfEurope mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_offset(25);
        make.top.equalTo(self.imageViewOfMap.mas_top).mas_offset(10);
        make.left.equalTo(self.imageViewOfMap.mas_left).mas_offset(SCREEN_WIDTH*0.3);
    }];
    [self.buttonOfOceania mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_offset(25);
        make.top.equalTo(self.imageViewOfMap.mas_top).mas_offset(100);
        make.right.equalTo(self.imageViewOfMap.mas_right).mas_offset(-SCREEN_WIDTH/10);
    }];
    [self.buttonOfSouthAmerica mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_offset(25);
        make.top.equalTo(self.imageViewOfMap.mas_top).mas_offset(100);
        make.left.equalTo(self.imageViewOfMap.mas_left).mas_offset(SCREEN_WIDTH/4);
    }];
    
    [self.buttonOfAsia addTarget:self action:@selector(goAsia) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonOfAfrica addTarget:self action:@selector(goAfrica) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonOfAntarctica addTarget:self action:@selector(goAntarctica) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonOfEurope addTarget:self action:@selector(goEurope) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonOfOceania addTarget:self action:@selector(goOceania) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonOfNorthAmerica addTarget:self action:@selector(goNorthAmerica) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonOfSouthAmerica addTarget:self action:@selector(goSouthAmerica) forControlEvents:UIControlEventTouchUpInside];
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.model.hot_country.count > 0 && self.model.country.count > 0) {
        return 2;
    }else if (self.model.hot_country.count == 0 && self.model.country.count == 0){
        return 0;
    }else{
        return 1;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.model.hot_country.count > 0 && indexPath.row == 0) {
        CellOfHotDestination *cell = [tableView dequeueReusableCellWithIdentifier:@"CellOfHotDestination" forIndexPath:indexPath];
        cell.model = self.model;
        return cell;
    }if (self.model.country.count > 0 && indexPath.row ==1 ) {
        CellOfOtherDestination *cell = [tableView dequeueReusableCellWithIdentifier:@"CellOfOtherDestination" forIndexPath:indexPath];
        cell.model = self.model;
        return cell;
    }else{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool" forIndexPath:indexPath];
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row ==0 && self.model.hot_country.count>0) {
        return self.model.hot_country.count/2 * 210 + self.model.hot_country.count%2 * 200 + 50;
    }else if (indexPath.row == 1 && self.model.country.count > 0) {
        return self.model.country.count * 45 + 60;
    }else{
        return 0;
    }
    }

- (void)goSearch{
    CommendSearchViewController *vc = [[CommendSearchViewController alloc]init];
    UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:vc];
    [FreeTravelNetwork GET:@"http://open.qyer.com/qyer/search/hot_history?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&page=1&track_app_channel=App%2520Store&track_app_version=7.0.1&track_device_info=iPhone%25205s&track_deviceid=E9CA001A-5603-4CA8-B2EA-2BE34F691AC6&track_os=ios%25209.3.4&v=1" andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        
        vc.arrayOfHot = [result objectForKey:@"data"];
    } andFailureBlock:^(NSError *error) {
        
    }];
    [self presentViewController:navi animated:YES completion:^{
        
    }];
}

- (void)goAsia{
    self.order = 0;
    [self handleData];
}
- (void)goAfrica{
    self.order = 5;
    [self handleData];
}
- (void)goAntarctica{
    self.order = 6;
    [self handleData];
}
- (void)goEurope{
    self.order = 1;
    [self handleData];
}
- (void)goOceania{
    self.order = 4;
    [self handleData];
}
- (void)goNorthAmerica{
    self.order = 2;
    [self handleData];
}
- (void)goSouthAmerica{
    self.order = 3;
    [self handleData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
